<template>
  <div class="edit">
    <div class="top">
      <p>编辑活动</p>
    </div>
    <div class="bottom">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item prop="name" label="名称:">
          <el-input v-model="form.name" size="medium" style="width: 300px" />
        </el-form-item>
        <el-form-item prop="start_at" label="活动时间">
          <el-date-picker
            v-model="value1"
            type="datetimerange"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            @change="datePickChange"
          />
        </el-form-item>
        <el-form-item prop="address" label="详情地址:">
          <el-input v-model="form.address" type="textarea" size="medium" style="width: 300px" />
        </el-form-item>
        <el-form-item prop="cover" label="封面图:">
          <el-upload
            action="http://ddsc.izhixiu.cn/api/common/upload"
            name="photo"
            :limit="1"
            :file-list="coverFileList"
            list-type="picture-card"
            :on-preview="handleCoverPreview"
            :on-remove="handleCoverRemove"
            :on-success="hanldeCoverSuccess"
          >
            <i class="el-icon-plus" />
          </el-upload>
          <p style="font-size:12px;color:#7B7B7B;">建议尺寸：750px*454px</p>
          <el-dialog :visible.sync="coverDialogVisible">
            <img width="100%" :src="coverDialogImageUrl" alt />
          </el-dialog>
        </el-form-item>
        <el-form-item prop="banners" label="轮播图:">
          <el-upload
            action="http://ddsc.izhixiu.cn/api/common/upload"
            name="photo"
            :file-list="bannersFileList"
            list-type="picture-card"
            :on-preview="handleCarouselPreview"
            :on-remove="handleCarouselRemove"
            :on-success="hanldeCarouselSuccess"
          >
            <i class="el-icon-plus" />
          </el-upload>
          <p style="font-size:12px;color:#7B7B7B;">建议尺寸：750px*454px</p>
          <el-dialog :visible.sync="carouselDialogVisible">
            <img width="100%" :src="carouselDialogImageUrl" alt />
          </el-dialog>
        </el-form-item>
        <el-form-item label="详情设置">
          <div class="tab">
            <div class="card" :class="{'active': activeName === 1}">
              <el-radio v-model="radio" :label="1" @change="handleChange(1)">跳转详情</el-radio>
            </div>
            <div class="card" :class="{'active': activeName === 2}">
              <el-radio v-model="radio" :label="2" @change="handleChange(2)">跳转链接</el-radio>
            </div>
          </div>
          <div class="tab-content">
            <div class="content">
              <div v-if="activeName === 1" class="tinymce-bo">
                <Tinymce v-model="form.detail" />
              </div>
              <div v-if="activeName === 2" class="link-box">
                <span>输入连接:</span>
                <el-input v-model="form.link" size="medium" style="width: 300px;margin-left: 20px" />
              </div>
            </div>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="save">
      <button class="submit" @click="submit">保存</button>
    </div>
  </div>
</template>
<script>
import Tinymce from "@/components/Tinymce";
import { addActivityItem, getActivityList } from "@/api/http/activity";
const ERR_OK = 200;
export default {
  components: {
    Tinymce
  },
  data() {
    return {
      bannersFileList: [],
      coverFileList: [],
      link: "",
      form: {
        name: "",
        cover: "",
        start_at: "",
        end_at: "",
        banners: [],
        address: "",
        detail: "",
        link: ""
      },
      value1: [],
      coverDialogVisible: false,
      coverDialogImageUrl: "",
      carouselDialogVisible: false,
      carouselDialogImageUrl: "",
      activeName: 1,
      radio: 1,
      rules: {
        name: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
        start_at: [{ required: true, message: "请输入时间", trigger: "blur" }],
        address: [
          { required: true, message: "请输入详情地址", trigger: "blur" }
        ],
        cover: [{ required: true, message: "请上传封面", trigger: "change" }],
        banners: [
          { required: true, message: "请上传轮播图", trigger: "change" }
        ]
      }
    };
  },
  created() {
    if (this.$route.query.id) {
      this._getActivityList(this.$route.query.id);
    }
  },
  methods: {
    datePickChange(val) {
      if (val) {
        this.form.start_at = val[0];
        this.form.end_at = val[1];
      } else {
        this.form.start_at = "";
        this.form.end_at = "";
      }
    },
    handleCoverPreview(file) {
      this.coverDialogImageUrl = file.url;
      this.coverDialogVisible = true;
    },
    handleCoverRemove(file, fileList) {
      this.coverDialogImageUrl = "";
      this.form.cover = "";
    },
    hanldeCoverSuccess(response, file, fileList) {
      if (response.code === ERR_OK) {
        this.form.cover = response.data.photo;
      }
    },
    handleCarouselPreview(file) {
      this.carouselDialogImageUrl = file.url;
      this.carouselDialogVisible = true;
    },
    handleCarouselRemove(file) {
      const fIndex = this.form.banners.findIndex(item => {
        return item === file.url;
      });
      if (fIndex >= 0) {
        this.form.banners.splice(fIndex, 1);
      }
      this.carouselDialogImageUrl = "";
    },
    hanldeCarouselSuccess(response, file, fileList) {
      if (response.code === ERR_OK) {
        this.form.banners.push(response.data.photo);
      }
    },
    handleChange(index) {
      this.radio = index;
      this.activeName = index;
    },
    submit() {
      const form = JSON.parse(JSON.stringify(this.form));
      if (this.activeName === 1) {
        delete form.link;
      } else if (this.activeName === 2) {
        delete form.detail;
      }
      if (this.$route.query.id) {
        form.id = this.$route.query.id;
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          this._addActivityItem(form).then(res => {
            if (res.code == 200) {
              this.$router.back();
            }
          });
        } else {
          return false;
        }
      });
    },
    _addActivityItem({
      name,
      cover,
      start_at,
      end_at,
      banners,
      address,
      detail,
      link,
      id
    }) {
      return addActivityItem({
        name,
        cover,
        start_at,
        end_at,
        banners,
        address,
        detail,
        link,
        id
      })
        .then(res => {
          if (res.code === ERR_OK) {
            if (id) {
              this.$message({
                message: "编辑成功",
                showClose: true,
                type: "success"
              });
            } else {
              this.$message({
                message: "添加成功",
                showClose: true,
                type: "success"
              });
            }

            return Promise.resolve(res);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    _getActivityList(id) {
      getActivityList("", "", "", id).then(res => {
        if (res.code === ERR_OK) {
          this.form.name = res.data[0].name;
          this.form.start_at = res.data[0].start_at;
          this.form.end_at = res.data[0].end_at;
          this.value1 = [res.data[0].start_at, res.data[0].end_at];
          this.form.cover = res.data[0].cover;
          this.coverFileList = [
            { name: res.data[0].cover.split(".com")[1], url: res.data[0].cover }
          ];
          this.form.address = res.data[0].address;
          this.form.banners = res.data[0].banners;
          res.data[0].banners.forEach((item, index) => {
            this.bannersFileList.push({
              name: item.split(".com")[1],
              url: item
            });
          });
          this.form.detail = res.data[0].detail;
          this.form.link = res.data[0].link;
          if (this.form.link) {
            this.activeName = 2;
            this.radio = 2;
          }
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
@import "@/styles/el-button.scss";
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  p {
    line-height: 44px;
    color: rgba(68, 68, 68, 1);
    font-size: 18px;
    text-align: left;
    font-family: Roboto;
    font-weight: bold;
  }
}
.bottom {
  .shop-wrapper {
    margin-top: 40px;
    margin-left: 10px;
    .row {
      display: flex;
      align-items: center;
      margin-top: 10px;
      .title {
        margin-right: 10px;
      }
      ul {
        display: flex;
        align-items: center;
        margin: 0;
        padding: 0;
        li {
          list-style: none;
          margin-right: 10px;
          span {
            line-height: 23px;
            color: rgba(102, 102, 102, 1);
            font-size: 14px;
            text-align: center;
            font-family: Roboto;
          }
        }
      }
    }
  }
}
.save {
  .submit {
    display: block;
    margin: 0 auto;
    width: 200px;
    line-height: 48px;
    border-radius: 6px;
    background-color: rgba(43, 51, 62, 1);
    text-align: center;
    font-size: 16px;
    color: #fff;
    border: none;
    outline: none;
  }
}
.tab {
  display: flex;
  align-items: center;
  .card {
    width: 120px;
    line-height: 41px;
    background-color: #fff;
    text-align: center;
    border: 1px solid rgba(255, 255, 255, 0);
    &.active {
      background-color: rgba(242, 242, 242, 1);
    }
  }
}
.tab-content {
  width: 993px;
  min-height: 143px;
  background-color: rgba(242, 242, 242, 1);
  text-align: center;
  border: 10px solid #f2f2f2;
  .link-box {
    display: flex;
    align-items: center;
    white-space: nowrap;
    margin-top: 30px;
  }
}
.mt {
  margin-top: 30px;
}
</style>
